﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Entity.AttributeManager;
using VOL.Entity.DomainModels.HealthEducation;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.IServices.HealthEducation;

namespace VOL.WebApi.Controllers.SmartMecial
{
    /// <summary>
    /// 广播管理控制器
    /// 负责处理广播相关的API请求
    /// </summary>
    [Route("api/BroadController")]
    [PermissionTable(Name = "BroadController")]
    public class BroadController : ControllerBase
    {
        private readonly IBroadManageService broadManageService;

        /// <summary>
        /// 构造函数，通过依赖注入获取广播管理服务实例
        /// </summary>
        /// <param name="broadManageService">广播管理服务</param>
        public BroadController(IBroadManageService broadManageService)
        {
            this.broadManageService = broadManageService;
        }

        /// <summary>
        /// 展示广播模板列表树形下拉框
        /// </summary>
        /// <returns>按层级结构组织的广播模板列表数据及操作结果</returns>
        [HttpGet("ShowBroadCastTemplateList")]
        public async Task<WebResponseContent> ShowBroadCastTemplateList()
        {
            return await broadManageService.ShowBroadCastTemplateList();
        }

        /// <summary>
        /// 添加宣教模板
        /// </summary>
        /// <param name="templateId">所属文件夹的ID</param>
        /// <returns>根据宣教模版父级文件夹的ID增加文件</returns>
        [HttpPost("CreateBroadCastTemplate")]
        public async Task<WebResponseContent> CreateBroadCastTemplate([FromBody] CreateBroadCastTemplateDTO createBroadCastTemplateDTO)
        {
            return await broadManageService.CreateBroadCastTemplate(createBroadCastTemplateDTO);
        }

        /// <summary>
        /// 父级下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetBroadCastTemplateDad")]
        public async Task<WebResponseContent> GetBroadCastTemplateDad() 
        {
            return await broadManageService.GetBroadCastTemplateDad();
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("UpdateFile")]
        public async Task<IActionResult> UpdateFile(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("未检测到上传文件");
            }

            // 七牛云账号、秘钥（建议用配置文件读取）
            var accessKey = "OecJqF2vFd_TrnrYa-xCJWu6u6hbKSY_YbXtV96i";
            var secretKey = "DbFMydQXQ_qK9QNHSvyH4cRHOxrMPIShzQsuBZGk";

            Mac mac = new Mac(accessKey, secretKey);
            var fileName = "MyImg/"+ $"{file.FileName}";

            using var fs = file.OpenReadStream();

            string Bucket = "battlefieldone";
            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 100;

            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            Config config = new Config();
            config.Zone = Zone.ZONE_CN_North;
            config.UseHttps = true;
            config.UseCdnDomains = true;
            config.ChunkSize = ChunkUnit.U512K;

            FormUploader fu = new FormUploader(config);
            var result = fu.UploadStream(fs, fileName, token, null);

            var imgUrl = "http://svgpl38o2.hb-bkt.clouddn.com/" + fileName;
            return Ok(new { url = imgUrl, result });
        }

        /// <summary>
        /// 修改宣教模板
        /// </summary>
        /// <param name="Id">需要修改的ID</param>
        /// <returns></returns>
        [HttpPost("UpdateBroadCastTemplate")]
        public async Task<WebResponseContent> UpdateBroadCastTemplate([FromBody]BroadCastTemplate broadCastTemplate) 
        {
        return await broadManageService.UpdateBroadCastTemplate(broadCastTemplate);
        }
        /// <summary>
        /// 逻辑删除宣教模板
        /// </summary>
        /// <param name="broadCastTemplateid"></param>
        /// <returns></returns>
        [HttpPost("DeleteBroadCastTemplate")]
        public async Task<WebResponseContent> DeleteBroadCastTemplate([FromBody]int broadCastTemplateid) 
        {
        return await broadManageService.DeleteBroadCastTemplate(broadCastTemplateid);
        }

        private readonly IBroadManageService _broadManageService;

        /// <summary>
        /// 添加广播管理列表
        /// </summary>
        /// <param name="broadCastManageList">广播管理对象列表</param>
        /// <returns>操作结果</returns>
        [HttpPost("AddBroadCastManageList")]
        public async Task<WebResponseContent> AddBroadCastManageList([FromBody] List<BroadCastManage> broadCastManageList)
        {
            return await _broadManageService.AddBroadCastManageList(broadCastManageList);
        }
        /// <summary>
        /// 修改广播管理列表
        /// </summary>
        /// <param name="broadCastManageList">广播管理对象列表</param>
        /// <returns>操作结果</returns>
        [HttpPost("UpdBroadCastManage")]
        public async Task<WebResponseContent> UpdBroadCastManage([FromBody] BroadCastManage broadCastManageList)
        {
            return await _broadManageService.UpdBroadCastManageList(broadCastManageList);
        }
        /// <summary>
        /// 获取广播列表
        /// </summary>
        /// <returns>广播列表数据</returns>
        [HttpGet("GetBroadList")]
        public async Task<WebResponseContent> GetBroadList()
        {
            return await _broadManageService.GetBroadList();
        }

        /// <summary>
        /// 获取广播模板列表
        /// </summary>
        /// <returns>广播模板列表数据</returns>
        [HttpGet("GetBroadCastTemplateList")]
        public async Task<WebResponseContent> GetBroadCastTemplateList()
        {
            return await _broadManageService.GetBroadCastTemplateList();
        }

        /// <summary>
        /// 获取广播类型列表
        /// </summary>
        /// <returns>广播类型列表数据</returns>
        [HttpGet("GetBroadTypeList")]
        public async Task<WebResponseContent> GetBroadTypeList()
        {
            return await _broadManageService.GetBroadTypeList();
        }

        /// <summary>
        /// 获取广播管理列表
        /// </summary>
        /// <returns>广播管理列表数据</returns>
        [HttpGet("GetBroadManageList")]
        public async Task<WebResponseContent> GetBroadManageList()
        {
            return await _broadManageService.GetBroadManageList();
        }
    }
}
